package cn.edu.scau.cmi.wujiaxian.client;

import java.util.ArrayList;
import java.util.List;

import cn.edu.scau.cmi.wujiaxian.adapter.AdapterOfClass;
import cn.edu.scau.cmi.wujiaxian.adapter.AdapterOfObject;
import cn.edu.scau.cmi.wujiaxian.adapter.SortStudentUtil;
import cn.edu.scau.cmi.wujiaxian.adapter.Student;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class AdapterClient extends Application {

	List<Student> studentList = new ArrayList<>();

	/**
	 * 开始方法
	 */
	@Override
	public void start(Stage primaryStage) {
		VBox root = new VBox(10);
		adapterTest0(root);

		Scene scene = new Scene(root, 700, 700);
		primaryStage.setTitle("适配器模式实验");
		primaryStage.setScene(scene);
		primaryStage.show();
	}

	/**
	 * 多例模式的实验
	 * 
	 * @param pane
	 */
	public void adapterTest0(Pane pane) {
		Button btn0 = new Button();
		btn0.setText("切换成类适配器");
		btn0.setOnAction(event -> {
			studentList.clear();
			classAdapter(pane);
		});
		pane.getChildren().add(btn0);

		Button btn1 = new Button();
		btn1.setText("切换成对象适配器");
		btn1.setOnAction(event -> {
			studentList.clear();
			objectAdapter(pane);
		});
		pane.getChildren().add(btn1);

		classAdapter(pane);
	}

	/**
	 * 类适配器模式
	 * 
	 * @param pane
	 */
	public void classAdapter(Pane pane) {
		pane.getChildren().remove(2, pane.getChildren().size());
		Text title = new Text("类适配器测试");
		pane.getChildren().add(title);

		TextField studentNumberTextField = new TextField();
		Label studentNumberLabel = new Label("学号：");
		pane.getChildren().addAll(studentNumberLabel, studentNumberTextField);

		TextField nameTextField = new TextField();
		Label nameLabel = new Label("名字：");
		pane.getChildren().addAll(nameLabel, nameTextField);

		Button btn = new Button();
		btn.setText("添加学生");
		btn.setOnAction(event -> {
			Student student = new Student(Integer.valueOf(studentNumberTextField.getText()), nameTextField.getText());
			SortStudentUtil sortStudentUtil = new AdapterOfClass();
			studentList.add(student);
			this.studentList = sortStudentUtil.sort(studentList);
			pane.getChildren().remove(8, pane.getChildren().size());
			for (Student student0 : studentList) {
				Text t = new Text();
				t.setText(student0.toString());
				pane.getChildren().add(t);
			}
		});

		pane.getChildren().add(btn);
	}

	/**
	 * 对象适配器模式
	 * 
	 * @param pane
	 */
	public void objectAdapter(Pane pane) {
		pane.getChildren().remove(2, pane.getChildren().size());
		Text title = new Text("对象适配器测试");
		pane.getChildren().add(title);

		TextField studentNumberTextField = new TextField();
		Label studentNumberLabel = new Label("学号：");
		pane.getChildren().addAll(studentNumberLabel, studentNumberTextField);

		TextField nameTextField = new TextField();
		Label nameLabel = new Label("名字：");
		pane.getChildren().addAll(nameLabel, nameTextField);

		Button btn = new Button();
		btn.setText("添加学生");
		btn.setOnAction(event -> {
			Student student = new Student(Integer.valueOf(studentNumberTextField.getText()), nameTextField.getText());
			SortStudentUtil sortStudentUtil = new AdapterOfObject();
			studentList.add(student);
			this.studentList = sortStudentUtil.sort(studentList);
			pane.getChildren().remove(8, pane.getChildren().size());
			for (Student student0 : studentList) {
				Text t = new Text();
				t.setText(student0.toString());
				pane.getChildren().add(t);
			}
		});

		pane.getChildren().add(btn);
	}

	public static void main(String[] args) {
//		launch(args);
		Platform.runLater(new Runnable() {
			public void run() {
				new AdapterClient().start(new Stage());
			}
		});
	}
}